//
//  MCDietVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/4.
//

import UIKit
import SnapKit
import QMUIKit
import AutoInch
import JXSegmentedView
import ObjectMapper
import Toast_Swift
import SwiftyJSON

class MCDietVc: MCBaseVc {
        
    var segmentedDataSource:JXSegmentedTitleDataSource!
    let segmentedView = JXSegmentedView()
    lazy var listContainerView:JXSegmentedListContainerView! = {
        return JXSegmentedListContainerView(dataSource: self)
    }()
    
    var dataList = [String]()
  
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        
        let headerNavigationView = UIView.init()
        let navititleLabel = QMUILabel.init()
        navititleLabel.text = "健康饮食"
        navititleLabel.font = UIFont(name: "PingFangHK-Semibold", size: 17)
        navititleLabel.textColor = UIColor.qmui_color(withHexString: "#10151C")
        headerNavigationView.addSubview(navititleLabel)
        navititleLabel.snp.makeConstraints { make in
            make.left.equalTo(headerNavigationView.snp.left).offset(16.0.auto())
            make.centerY.equalTo(headerNavigationView.snp.centerY).offset(0.0)
        }
        self.navigationItem.titleView = headerNavigationView
        
        //scrollView 滚动页面
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.isTitleColorGradientEnabled = true
        
        segmentedView.dataSource = segmentedDataSource
        segmentedView.delegate = self
        
       
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = 20
        segmentedView.indicators = [indicator]
        view.addSubview(segmentedView)
        
        segmentedView.listContainer = listContainerView
        
        view.addSubview(listContainerView)
        
        segmentedView.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(0)
            make.right.equalTo(view.snp.right).offset(0)
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(0.0)
            make.height.equalTo(50.auto())
        }
        
        listContainerView.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(0)
            make.right.equalTo(view.snp.right).offset(0)
            make.top.equalTo(segmentedView.snp.bottom).offset(0.0)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(0.0)
        }
        
        getListLabels()
    }
    
    
    
    func getListLabels() -> Void {
        NetWorkRequest(MCDietApi.dietLabelCategory) { [self]ResponseModel in
            print(ResponseModel.dataString)
            var categorylist = Mapper<MCdietCategoryModel>().mapArray(JSONString: ResponseModel.dataString)
            dataList = (categorylist?.map({ MCdietCategoryModel in
                return MCdietCategoryModel.labelName ?? ""
            }))!
            dataList.insert("推荐", at: 0)
            dataList.append("食材")
            self.segmentedDataSource.titles = dataList
            self.segmentedView.reloadDataWithoutListContainer()
            self.segmentedView.reloadData()
        } failureCallback: { ResponseModel in
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
    }
    
}

extension MCDietVc:JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {

    }
}

extension MCDietVc:JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView:JXSegmentedListContainerView) -> Int {
        return self.dataList.count
    }
    
    func listContainerView(_ listContainerView:JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        let labelName = self.dataList[index]
        
        if labelName == "食材" {
            let dietBaseVc = MCFoodBaseVc.init()
             
            return dietBaseVc
        }else{
            let dietBaseVc = MCDietBaseVc.init()
            dietBaseVc.indexName = labelName
            dietBaseVc.label = "\(index)"
            return dietBaseVc
        }
        
        
    }
    
}


